package com.neusoft.mapper;

import java.sql.Date;
import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.neusoft.po.CalendarPO;
import com.neusoft.vo.ApproveAttendanceVO;
import com.neusoft.vo.Clock;
import com.neusoft.vo.WorkType;
import com.neusoft.vo.insertNextDay4EmpsVO;

public interface ClockMapper {

	// 页面载入时显示当前打卡情况
	@Select("SELECT * FROM `attendance` WHERE empno = #{empno} AND date = #{date}")
	Clock searchClock(Clock clock);

	// 上班打卡
	@Update("UPDATE attendance SET clockIn_time = #{clockIn_time} WHERE empno = #{empno} AND date = #{date}")
	int clockIn(Clock clock);

	// 下班打卡
	@Update("UPDATE attendance SET clockOut_time = #{clockOut_time} WHERE empno = #{empno} AND date = #{date}")
	int clockOut(Clock clock);

	// 查询员工排班情况
	@Select("SELECT work_type, workIn_time , workOut_time FROM emp e JOIN work_type w ON (e.work_id = w.work_id) WHERE empno = #{id}")
	WorkType getWorkType(int empno);

	// 查询当天打卡情况
	@Select("select * FROM attendance WHERE empno = #{empno} AND date = #{date}")
	Clock getclock(Clock clock);

	// 设置考勤结果
	@Update("UPDATE `attendance` SET state = #{state} WHERE empno=#{empno} and date = #{date}")
	int setState(Clock clock);

	// 生成补签数据
	@Insert("INSERT INTO reattendace VALUES(#{attend_id},#{empno},#{date},#{clockIn_time},#{clockOut_time},NULL,-2)")
	int InsertReattendace(Clock clock);

	// ===================补签====================================================================
//    页面载入时 自动查询当前员工所需补签的所有数据
	@Select("select r.* , ename , a.state  \n" + "            FROM attendance a  \n"
			+ "            join reattendace r  ON(a.attend_id =r.attend_id) \n"
			+ "            JOIN emp e ON(r.empno = e.empno) \n"
			+ "            WHERE r.empno = #{id} AND (restate IN (-2,0))")
	List<Clock> selectReattendace(int empno);

	// 单击补签后 将信息存入reattendace表
	@Update("UPDATE reattendace SET clockIn_time = #{clockIn_time} ,clockOut_time = #{clockOut_time} , reattend_reason = #{reattend_reason} ,restate = -1 where date = #{date} AND empno = #{empno}")
	int reattendaceCount(Clock clock);

	// ====================审核补签====================================================================
//	为审批者显示所有待审核补签信息总数
	@Select("SELECT COUNT(*) FROM reattendace r JOIN emp e ON(r.empno=e.empno) WHERE deptno = #{deptno} AND restate = -1")
	int selectCount(ApproveAttendanceVO vo);

//    为审批者显示所有待审核补签信息
	@Select("SELECT * FROM reattendace r JOIN emp e ON(r.empno=e.empno) WHERE deptno = #{deptno} AND restate = -1 LIMIT #{currentPage},#{pageSize}")
	List<Clock> selectAllEmpReattendance(ApproveAttendanceVO vo);

	// 单击“同意”按钮时，将该条数据的restate置为 1，将state置为8
	@Update("UPDATE reattendace \n" + "SET restate = 1 \n" + "WHERE attend_id = #{id}")
	int agree(Integer attend_id);

	@Update("UPDATE attendance \n" + "SET state = 8 \n" + "WHERE attend_id = #{id}")
	int agree2(Integer attend_id);

	// 单击“驳回”按钮时，将该条数据的restate置为 0
	@Update("UPDATE reattendace \n" + "SET restate = 0 \n" + "WHERE attend_id = #{id}")
	int refuse(Integer attend_id);

	// 每日00：00：00定时任务================================================================
	// 1.查出所有emp
	@Select("SELECT empno FROM `emp`")
	List<insertNextDay4EmpsVO> selectAllEmp();

	@Select("select * from calendar where date = #{date}")
	CalendarPO selectDate_typeByDate(Date date);

	// 2.为所有empno添加次日考勤记录
	@Insert("INSERT INTO attendance (empno, date, state)\r\n" + "SELECT #{empno}, #{date}, #{state}\r\n"
			+ "WHERE NOT EXISTS (\r\n" + "    SELECT *\r\n" + "    FROM attendance\r\n"
			+ "    WHERE empno = #{empno} AND date = #{date}\r\n" + ");")
	int insertNextDay4Emps(insertNextDay4EmpsVO vo);
}
